//当非法进入页面时 跳转回首页或之前页面
$(function () {
    //获取 url 中? 及后面的内容
    if (!location.search) {
        if (history.back) {
            location.href = "http://localhost/mi/pages/index.html"
        } else {
            history.back()
        }
    }
})
//获取 商品详情
let price;
let stock;
$.ajax({
    url: '../server/detail.php',
    data: location.search.substring(1),
    dataType: "json",
    async: false,
    success(res) {
        if (res.meta.status == 0) {
            //从后台拿到数据
            var detArr = res.data
            price = detArr.price
            stock = detArr.stock
            //所有图片
            imgArr = detArr.imgpath.split('=')
            let arr1 = '';
            imgArr.forEach(v => {
                arr1 += `<div class="swiper-slide" style="background:#fff"><a href="javascript:;"><img src="${v}" alt=""></a> </div>`
            })
            //轮播图
            $('.swiper-aaa').html(arr1)
            $('.content').html(` 
            <h5>${detArr.name}</h5>
            <p>${detArr.introduce}</p>
            <h6>${detArr.price}元</h6>
            <div>
                <input id="reduce" type="button" value="-">
                <input id="num" type="text" value="1">
                <input id="add" type="button" value="+">
            </div>
            <span>库存：${detArr.stock}</span>
            <h6>总计：<span>${detArr.price}</span>元</h6>
            <button>加入购物车</button>
            `)
        }
    }
})
// -
$('#reduce').click(function () {
    if ($('#num').val() * 1 <= 1) {
        layer.msg("最少可购买一件商品")
    } else {
        $('#num').val($('#num').val() - 1)
        $('h6>span').html($('#num').val() * price)
    }
})
// +
$('#add').click(function () {
    if ($('#num').val() * 1 + 1 > stock) {
        layer.msg("超出库存")
    } else {
        $('#num').val($('#num').val() - 0 + 1)
        $('h6>span').html($('#num').val() * price)
    }
})
// 输入数量
$('#num').on('blur', function () {
    if ($('#num').val() * 1 <= 0) {
        $('#num').val(1)
        layer.msg("最少可购买一件商品")
        $('h6>span').html($('#num').val() * price)
    }
    if ($('#num').val() * 1 > stock) {
        layer.msg('超出库存')
        $('#num').val(stock)
        $('h6>span').html($('#num').val() * price)
    } else {
        $('h6>span').html($('#num').val() * price)
    }
})
//加入购物车
$('.detail button').click(function () {
    if (isLogin()) {
        var cartArr = JSON.parse(localStorage.getItem('cart'))
        var cart = {
            username: isLogin(),
            id: location.search.substring(4),
            num: $('#num').val() * 1
        }
        //本地存储无购物车数据
        if (!cartArr) {
            localStorage.setItem("cart", `[${JSON.stringify(cart)}]`)
            location.href = 'cart.html'
        } else { //有数据 
            let index = cartArr.findIndex(v => v.username === cart.username && v.id === cart.id);
            //购物车中没有这个用户的这个商品
            if (index == -1) {
                cartArr.push(cart)
                localStorage.setItem("cart", JSON.stringify(cartArr))
                location.href = 'cart.html'
            } else {
                cartArr.forEach(v => {
                    if (v.username === cart.username && v.id === cart.id) {
                        console.log("aaa")
                        var addnum = v.num * 1 + cart.num * 1
                        if (addnum > stock) {
                            layer.msg("超出库存")
                            v.num = stock
                        } else {
                            v.num = addnum
                        }
                    }
                });
                localStorage.setItem("cart", JSON.stringify(cartArr))
                location.href = 'cart.html'
            }

        }
    } else {
        layer.msg('您还没有登录')
    }
})

//轮播图
var swiper = new Swiper(".mySwiper", {
    spaceBetween: 30,
    effect: "fade",
    setWrapperSize: true,
    loop: true,
    autoplay: {
        delay: 3000, // 自动轮播切换的间隔时间
        pauseOnMouseEnter: true, // 鼠标移入停止轮播
        disableOnInteraction: false //鼠标移出继续轮播
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    },
});